/* eslint-disable eol-last */
/* eslint-disable indent */
'use strict';

const { createBlogFailInfo } = require('../utils/ErrorModel');
const {
    SuccessModel,
    ErrorModel,
} = require('../utils/resultModel');
const PAGE_SIZE = 5;

module.exports = app => {
    class BlogController extends app.Controller {
        async createBlog() {
                const { ctx } = this;
                // 3.获取提交的数据  内容和地址
                const { content, images } = ctx.request.body;
                // 还需要userId
                const { id: userId } = ctx.session.getUserInfo;

                const REG_AT_WHO = /@(.+?)\s-\s(\w+?)\b /g;
                // 获取 at 用户名列表
                const atUserNameList = [];

                content.replace(REG_AT_WHO,
                    (matchStr, nickName, userName) => {
                        atUserNameList.push(userName);
                        return matchStr;
                    });
                // Promise.all() => []
                // 获取用户列表
                const atUserList = await Promise.all(atUserNameList.map(async userName => {
                    const result = await this.service.user.getUserInfo(userName);
                    return result;
                }));

                // 获取用户id列表
                const atUserIdList = atUserList.map(user => user.id);
                try {
                    // 4.将数据交给service处理
                    const result = await this.service.blog.create({ content, userId, images });
                    // 向 atRelation 表中写数据
                    atUserIdList.map(async userId => {
                        await this.service.atRelation.createAtRelation(result.id, userId);
                    });
                    ctx.body = new SuccessModel(result);
                } catch (err) {
                    console.log(err);
                    ctx.body = new ErrorModel(createBlogFailInfo);
                }
            }
            // 加载更多
        async loadProfileMore() {
            // 获取参数
            let { pageIndex } = this.ctx.params;
            pageIndex = parseInt(pageIndex);
            // 定义变量userId 值是用户的id
            const { userName } = this.ctx.session.getUserInfo;
            const pageSize = PAGE_SIZE;
            // 获取博客信息 调用service中的方法
            const result = await this.service.blog.getProfileBlogList({ userName, pageIndex, pageSize });
            this.ctx.body = new SuccessModel({
                blogList: result.blogList,
                count: result.count,
                pageIndex: pageIndex + 1,
                pageSize,
            });
        }


        // 加载广场页更多数据
        async loadSquareMore() {
                // 获取参数
                let { pageIndex } = this.ctx.params;
                pageIndex = parseInt(pageIndex);
                // 定义变量userId 值是用户的id
                const pageSize = 10;
                // 获取博客信息 调用service中的方法
                const result = await this.service.blog.getSquareBlogList({ pageIndex, pageSize });
                // 响应数据
                this.ctx.body = new SuccessModel({
                    blogList: result.blogList,
                    count: result.count,
                    pageIndex,
                    pageSize,
                });
            }
            // 首页加载更多
        async loadHomeBlogMore() {
                // 在session中获取用户id
                const { id } = this.ctx.session.getUserInfo;
                // 获取pageIndex
                let { pageIndex } = this.ctx.params;
                pageIndex = parseInt(pageIndex);
                // 获取关注人博客
                const result = await this.service.blog.getFollowerBlogList({ userId: id, pageIndex });
                // 响应数据
                console.log('页码数' + pageIndex);
                this.ctx.body = new SuccessModel({
                    blogList: result.blogList,
                    count: result.count,
                    pageIndex: pageIndex + 1,
                    pageSize: 10,
                });
            }
            // atMe加载更多
        async loadAtMeBlogMare() {
            // userId
            const { id: userId } = this.ctx.session.getUserInfo;
            // pageIndex
            const { pageIndex } = this.ctx.params;
            // getAtMeBlogList({userId,pageIndex,pageSize})
            const result = await this.service.atRelation.getAtMeBlogList({ userId, pageIndex });
            this.ctx.body = new SuccessModel({
                blogList: result.blogList,
                count: result.count,
                pageIndex: pageIndex + 1,
                pageSize: 10,
            });
        }
    }
    return BlogController;
};